<?php

namespace app\api\controller;

use think\facade\Request;
use think\DB;
use app\model\BuyerExchange;
use app\model\Withdraw;
use think\Validate;
use app\model\MemberBuyer;


class Wallet
{
    public function recording()
    {
        $dataGet = Request::get('');
        $dataGet['userid'] = session('memberInfo.userid');
        $data = BuyerExchange::getExchange($dataGet);
        return json([
            'code' => 0,
            'data' => $data
        ]);
    }

    public function withdraw()
    {
        $data = \app\model\Withdraw::getWithdraw([
            'userid' => session('memberInfo.userid'),
            // 'type'=>Request::get('type'),
        ]);
        foreach ($data as $vo) {
            $vo['addtime'] = substr($vo['addtime'], 0, 10);
            $vo['optime'] = substr($vo['optime'], 0, 10);
        }
        return json([
            'code' => 0,
            'data' => $data
        ]);
    }

    public function withdraw_post()
    {
        $memberBuyer = MemberBuyer::where('userid', session('memberInfo.userid'))->find();
        if ($memberBuyer->getData('idverifystatus') != 2) {
            return json(['code' => 4030, 'msg' => lang(4030)]);
        }

        $validate = Validate::make([
            'type' => 'require|integer',
            'amount' => 'require|float|gt:0',
            'password' => 'require|length:6,32',
        ])->message([
            'type' => '请选择提现类型',
            'amount.require' => '请输入提现金额',
            'amount.float' => '提现金额为数值',
            'amount.gt' => '提现金额应大于0',
            'password.require' => '请输入登录密码',
            'password.length' => '登录密码必须大于6位数',
        ]);

        if (Request::post('type') == 1) {
            $row = Withdraw::where('status', '<>', '3')
                ->where('addtime', '>', date('Y-m-d'))
                ->where('userid', session('memberInfo.userid'))
                ->where('usertype', 1)
                ->where('type', 1)
                ->find();

            if ($row) {
                return json([
                    'code' => 1,
                    'msg' => '每天只可申请一次'
                ]);
            }

            $row = Withdraw::where('status', 'in', '0,1')
                ->where('userid', session('memberInfo.userid'))
                ->where('usertype', 1)
                ->where('type', 1)
                ->find();

            if ($row) {
                return json([
                    'code' => 1,
                    'msg' => '当前有正在进行的申请'
                ]);
            }
        }

        if (Request::post('type') == 2) {
            $row = Withdraw::where('status', '<>', '3')
                ->where('addtime', '>', date('Y-m-d'))
                ->where('userid', session('memberInfo.userid'))
                ->where('usertype', 1)
                ->where('type', 2)
                ->find();

            if ($row) {
                return json([
                    'code' => 1,
                    'msg' => '每天只可申请一次'
                ]);
            }

            $row = Withdraw::where('status', 'in', '0,1')
                ->where('userid', session('memberInfo.userid'))
                ->where('usertype', 1)
                ->where('type', 2)
                ->find();

            if ($row) {
                return json([
                    'code' => 1,
                    'msg' => '当前有正在进行的申请'
                ]);
            }
        }

        if (!$validate->check(Request::post(''))) {
            return json([
                'code' => 1,
                'msg' => $validate->getError()
            ]);
        }
        if (!MemberBuyer::verifyPssword(Request::post('password'), session('memberInfo.password'))) {
            return json([
                'code' => 1,
                'msg' => '登录密码不正确'
            ]);
        }



        $payExchange = new BuyerExchange();
        if (Request::post('type') == 1) {
            if (!$payExchange->reduceAmount(session('memberInfo.userid'), Request::post('amount'), '本金提现')) {
                return json(['code' => 1, 'msg' => $payExchange->errorMsg]);
            }
        }
        if (Request::post('type') == 2) {
            if (!$payExchange->reduceCoin(session('memberInfo.userid'), Request::post('amount'), '佣金提现')) {
                return json(['code' => 1, 'msg' => $payExchange->errorMsg]);
            }
        }

        $affectedRows = DB::name('withdraw')->insert([
            'userid' => session('memberInfo.userid'),
            'amount' => Request::post('amount'),
            'bankaccountname' => $memberBuyer['bankaccountname'],
            'type' => Request::post('type'),
            'bankname' => $memberBuyer['bankname'],
            'bankaccount' => $memberBuyer['bankaccount'],
            'usertype' => 1,
        ]);

        if ($affectedRows !== false) {
            return json(['code' => 0]);
        } else {
            return json(['code' => 1, 'msg' => '操作失败']);
        }
    }

}
